import {Base} from '../../utils/base.js';

class Order extends Base{

  constructor(){
    super();
    this._storageKeyName = 'newOrder';
  }

  /**下订单 */
  doOrder(param,callback){
    var that = this;
    var allParams = {
      url : 'order',
      type : 'POST',
      data: { products: JSON.stringify(param)},
      sCallback: function(data){
        that.execSetStorageSync(true);
        callback && callback(data);
      },
      eCallback : function(){

      }
    };
    this.request(allParams);
  }

  /*
   *拉起维信支付
   * callback回调函数  0商品缺货等原因导致订单不能支付
   * 1 支付失败或者支付取消
   * 2 支付成功
   */
  execPay(orderNumber,callback){
    callback && callback(2);
  }

  //获得订单具体的具体内容
  getOrderInfoById(id,callback){
    var that = this;
    var allParams ={
      url:'order/'+id,
      sCallback:function(data){
        callback && callback(data);
      },
      eCallback:function(){

      }
    };
    this.request(allParams);
  }

  getOrderInfoByOrderId(id, callback){
    var that = this;
    var allParams = {
      url: 'orderid/' + id,
      sCallback: function (data) {
        callback && callback(data);
      },
      eCallback: function () {

      }
    };
    this.request(allParams);
  }

  /**获得所有订单，pageIndex 从1开始 */
  getOrders(pageIndex, callback){

    var allParams = {
      url:'order/by_user',
      data: { page: pageIndex},
      type:'get',
      sCallback:function(data){
        callback && callback(data);
      }
    };
    this.request(allParams);
    
  }

  /**本地缓存  保存/更新 */
  execSetStorageSync(data){
    wx.setStorageSync(this._storageKeyName, data);
  }

/**是否有新的订单 */
  hasNewOrder(){
    var result = false;
    var flag = wx.getStorageSync(this._storageKeyName);
    if (flag==null){
      result = true
    }
    return true;
  }
}

export {Order}